package com.hc.file.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;

import com.hc.map.MapArea;
import com.hc.map.MapAreaDot;
import com.hc.util.CommonData;
import com.hc.util.DataBase;

public class SystemConfigUtil {
	public static void readConfig(){
	File file = new File(CommonData.systemPath + "config"+File.separator+"systemConfig.properties");
		FileReader fileReader = null;
			try {
				fileReader = new  FileReader(file);
			} catch (FileNotFoundException e1) {
					e1.printStackTrace();
			}
			BufferedReader reader = new BufferedReader(fileReader);
			String linStr = "";
			
			try {
				while((linStr=reader.readLine())!=null){
					
				String[] results= 	linStr.split("=");
					
					if("mapFilePath".equals(results[0])){
						
						CommonData.mapFilePath = results[1];
					}
					else if("mapConfigFilePath".equals(results[0])){
						
						CommonData.mapConfigFilePath = results[1];
					}else if("mapscale".equals(results[0])){
						
						CommonData.mapscale = Integer.parseInt(results[1]);
					}else if("mapscaleUnit".equals(results[0])){
						
						CommonData.mapscaleUnit = Integer.parseInt(results[1]);
					}else if("isUseKalmanMath".equals(results[0])){
						
						CommonData.isUseKalmanMath = Boolean.parseBoolean(results[1]);
						
					}else if("dataBaseIp".equals(results[0])){
						
						DataBase.ip = results[1];
						
					}else if("dataBasePort".equals(results[0])){
						DataBase.port = results[1];
						
					}else if("dataBasePwd".equals(results[0])){
						
						DataBase.pwd = results[1];
					}else if("dataBaseUserName".equals(results[0])){
						DataBase.userName = results[1];
						
					}else if("dataBaseDataBaseName".equals(results[0])){
						
						DataBase.dataBaseName = results[1];
					}else if("useDataBase".equals(results[0])){
						
						CommonData.useDataBase = Boolean.parseBoolean(results[1]);
						
					}else if("area1".equals(results[0])){
						initAreaMap("area1", results[1]);
					}else if("area2".equals(results[0])){
						initAreaMap("area2", results[1]);
					}else if("area3".equals(results[0])){
						initAreaMap("area3", results[1]);
					}else if("area4".equals(results[0])){
						initAreaMap("area4", results[1]);
					}else if("area5".equals(results[0])){
						initAreaMap("area5", results[1]);
					}
				}
				
				reader.close();
				fileReader.close();
			} catch (IOException e) {
					e.printStackTrace();
			}


	}
	private static void initAreaMap(String area, String info){
		int areaId = Integer.parseInt(area.substring(area.length() - 1, area.length()));
		String[] xy = info.split(";");
		int x1 = Integer.parseInt(xy[0].split(",")[0]);
		int y1 = Integer.parseInt(xy[0].split(",")[1]);
		
		int x2 = Integer.parseInt(xy[1].split(",")[0]);
		int y2 = Integer.parseInt(xy[1].split(",")[1]);
		
		int x3 = Integer.parseInt(xy[2].split(",")[0]);
		int y3 = Integer.parseInt(xy[2].split(",")[1]);
		
		int x4 = Integer.parseInt(xy[3].split(",")[0]);
		int y4 = Integer.parseInt(xy[3].split(",")[1]);
		MapArea mapArea = new MapArea(areaId, new MapAreaDot(x1, y1), new MapAreaDot(x2, y2), new MapAreaDot(x3, y3), new MapAreaDot(x4, y4));
		CommonData.areaMap.put(areaId, mapArea);
	}
	
	private static String getAreaString(String lineSeparator){
		StringBuffer sb = new StringBuffer();
		//area1=100,100;400,100;600,500;200,350
		for(Map.Entry<Integer, MapArea> entry : CommonData.areaMap.entrySet()){
			MapArea area = entry.getValue();
			sb.append("area" + entry.getKey());
			sb.append("=");
			sb.append(area.getDot1().getX() + "," + area.getDot1().getY() + ";");
			sb.append(area.getDot2().getX() + "," + area.getDot2().getY() + ";");
			sb.append(area.getDot3().getX() + "," + area.getDot3().getY() + ";");
			sb.append(area.getDot4().getX() + "," + area.getDot4().getY() + ";");
			sb.append(lineSeparator);
		}
		return sb.toString();
	}
	
	public static void writeConfig(){
		StringBuilder sb = new StringBuilder();
		String lineSeparator = System.getProperty("line.separator", "\n");  
			sb.append("mapFilePath=").append(CommonData.mapFilePath).append(lineSeparator);
			sb.append("mapConfigFilePath=").append(CommonData.mapConfigFilePath).append(lineSeparator);
			sb.append("mapscale=").append(CommonData.mapscale).append(lineSeparator);
			sb.append("mapscaleUnit=").append(CommonData.mapscaleUnit).append(lineSeparator);
			sb.append("isUseKalmanMath=").append(CommonData.isUseKalmanMath).append(lineSeparator);
			
			
			sb.append("dataBaseIp=").append(DataBase.ip ).append(lineSeparator);
			sb.append("dataBasePort=").append(DataBase.port).append(lineSeparator);
			sb.append("dataBasePwd=").append(DataBase.pwd).append(lineSeparator);
			sb.append("dataBaseUserName=").append(DataBase.userName).append(lineSeparator);
			sb.append("dataBaseDataBaseName=").append(DataBase.dataBaseName).append(lineSeparator);
			sb.append("useDataBase=").append(CommonData.useDataBase).append(lineSeparator);
			sb.append(getAreaString(lineSeparator));
			
			
		File file = new File(CommonData.systemPath + "config" + File.separator + "systemConfig.properties");
		FileWriter w;
		try {
			w = new FileWriter(file);
			w.write(sb.toString());
			w.flush();
			w.close();
		} catch (IOException e) {
			e.printStackTrace();
			
		}
	}
	public static void main(String[] args) {
	
		SystemConfigUtil.readConfig();
		SystemConfigUtil.writeConfig();
	
	}

}
